<template>
    <div class="views-liuyanban-list">
        <div>
            <el-card class="box-card">
                <template #header>
                    <div class="clearfix">
                        <span class="title"> 留言板查询 </span>
                    </div>
                </template>

                <el-table border :data="lists" style="width: 100%" @sort-change="sortChange" highlight-current-row>
                    <el-table-column type="index" label="#"></el-table-column>
                    <!-- 序号 -->

                    <el-table-column prop="xingming" label="姓名" width="130">
                        <template #default="{row}"> {{ row.xingming }}</template>
                    </el-table-column>
                    <el-table-column prop="lianxidianhua" label="联系电话" width="130">
                        <template #default="{row}"> {{ row.lianxidianhua }}</template>
                    </el-table-column>
                    <el-table-column prop="liuyanneirong" label="留言内容">
                        <template #default="{row}"> {{ row.liuyanneirong }}</template>
                    </el-table-column>
                    <el-table-column prop="liuyanzhuangtai" label="留言状态" width="130">
                        <template #default="{row}"> {{ row.liuyanzhuangtai }}</template>
                    </el-table-column>
                    <el-table-column prop="liuyanren" label="留言人" width="80">
                        <template #default="{row}"> {{ row.liuyanren }}</template>
                    </el-table-column>
                    <el-table-column prop="huifuneirong" label="回复内容">
                        <template #default="{row}"> {{ row.huifuneirong }}</template>
                    </el-table-column>


                    <el-table-column label="操作" fixed="right" width="180">
                        <template #default="{row}">
                            <el-button-group>
                                <el-tooltip effect="dark" content="回复" placement="top-start"
                                >
                                    <el-button type="success" :icon="Edit" size="small"
                                               @click="$router.push('/admin/liuyanban/updt?id='+row.id)"></el-button>
                                </el-tooltip>
                                <el-tooltip effect="dark" content="删除" placement="top-start"
                                >
                                    <el-button type="danger" :icon="Delete" size="small"
                                               @click="deleteItems(row.id)"></el-button>
                                </el-tooltip>
                            </el-button-group>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="e-pages" style="margin-top: 10px; text-align: center">
                    <el-pagination
                            @current-change="loadList"
                            :page-sizes="[10, 20, 30, 40,50]"
                            v-model:current-page="search.page"
                            v-model:page-size="search.pagesize"
                            @size-change="sizeChange"
                            layout="total, sizes, prev, pager, next, jumper"
                            :total="totalCount"
                    >
                    </el-pagination>
                </div>
            </el-card>
        </div>
    </div>
</template>

<script setup>
    import http from "@/utils/ajax/http";
    import DB from "@/utils/db";
    import router from "@/router";

    import {ref, reactive, watch, unref, onBeforeMount} from "vue";
    import {useRoute} from "vue-router";
    import {session} from "@/utils/utils";
    import {canLiuyanbanSelect, useLiuyanbanSelect, canLiuyanbanDelete} from "@/module";
    import {extend} from "@/utils/extend";
    import {ElMessageBox, ElMessage} from "element-plus";
    import {Edit, Delete} from "@element-plus/icons-vue";

    const route = useRoute();
    const search = reactive({
        page: 1, // 当前页
        pagesize: 10, // 每页行数
        orderby: "id", // 排序字段
        sort: "desc", // 排序类型
    });
    extend(search, route.query);
    // 链接参数变化时更新这些内容
    watch(
        () => route.query,
        () => {
            extend(search, route.query);
            loadList(1);
        },
        {deep: true}
    );

    // 总行数
    const totalCount = ref(0);
    // 列表数据
    const lists = ref([]);
    // 加载状态
    const loading = ref(false);

    // 排序操作
    const sortChange = (e) => {
        console.log(e);
        if (e.order == null) {
            search.orderby = "id";
            search.sort = "desc";
        } else {
            search.orderby = e.prop;
            search.sort = e.order == "ascending" ? "asc" : "desc";
        }
        loadList(1);
    };
    // 设置页数多少
    const sizeChange = (e) => {
        search.pagesize = e;
        loadList(1);
    };

    const deleteItems = (ids) => {
        return new Promise((resolve, reject) => {
            ElMessageBox.confirm("确定删除？")
                .then((res) => {
                    canLiuyanbanDelete(ids).then((res) => {
                        loadList(search.page);
                        resolve(res.data);
                    });
                })
                .catch((err) => {
                    reject(err);
                });
        });
    };

    // 加载留言板列表方法
    const loadList = (page) => {
        // 加载
        if (unref(loading)) return;
        loading.value = true;
        search.page = page;

        http.post("/api/liuyanban/selectPages", search).then(
            (res) => {
                loading.value = false;
                if (res.code == 0) {
                    var data = res.data;
                    lists.value = data.lists.records;
                    totalCount.value = data.lists.total;
                }
            },
            (err) => {
                ElMessage.error(err.message);
            }
        );
    };

    onBeforeMount(() => {
        loadList(1);
    });
</script>

<style scoped lang="scss">
    .views-liuyanban-list {
    }
</style>
